//
//  ZSY_MyCollectionViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 16/1/12.
//  Copyright © 2016年 张诗宇. All rights reserved.
//

#import "ZSY_MyCollectionViewController.h"
#import "ZSY_CityModel.h"
#import "ZSY_CityHunterDetailViewController.h"

@interface ZSY_MyCollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) ZSY_BaseTableView *myTableView;
@property (nonatomic, retain) NSMutableArray *allArray;

@end

@implementation ZSY_MyCollectionViewController

- (void)dealloc {
    [_allArray release];
    [_myTableView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.allArray = [NSMutableArray array];
    self.allArray = [[ZSY_DataBaseManager shareDataBase] selectCity];
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:_myTableView];
    
    [_myTableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    ZSY_BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ZSY_BaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    
    ZSY_CityModel *model = [_allArray objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allArray.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZSY_CityHunterDetailViewController *vc = [[ZSY_CityHunterDetailViewController alloc] init];
    ZSY_CityModel *model = [_allArray objectAtIndex:indexPath.row];
    vc.cityModel = model;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];

}


#pragma mark - 删除收藏
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    self.allArray = [[DataBaseSingleton shareDataBase] selectMovie];
    ZSY_CityModel *city = [self.allArray objectAtIndex:indexPath.row];
    [[ZSY_DataBaseManager shareDataBase] deleteCityTable:city];
    
    [self.allArray removeObjectAtIndex:indexPath.row];
    
    
    NSLog(@"movie = %@", city.title);
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
